package com.justplay.mobilelocation;


import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.justplay.mobilelocation.UI.ContactsActivity;
import com.justplay.mobilelocation.util.Config;
import com.justplay.mobilelocation.util.InputActivityUtil;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.ErrorFunListener;
import com.sunshine.retrofit.interfaces.Success;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;


/**
 * A simple {@link Fragment} subclass.
 */
public class SmsFragment extends Fragment {


    @Bind(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @Bind(R.id.tv_noData)
    TextView tv_noData;
    @Bind(R.id.iv_smsSample)
    ImageView iv_smsSample;
    @Bind(R.id.btn_sample)
    Button btn_sample;
    @Bind(R.id.spinner)
    Spinner spinner;
    @Bind(R.id.tv_right_2)
    TextView tv_right_2;
    @Bind(R.id.iv_contast)
    ImageView iv_contast;
    @Bind(R.id.et_inputNumber)
    EditText et_inputNumber;

    public SmsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_sms, container, false);
        ButterKnife.bind(this, view);
        initView();
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {

                Toast.makeText(getActivity(), "正在加载数据", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        mSwipeRefreshLayout.setRefreshing(false);
                        Toast.makeText(getActivity(), "你没有数据保存在网络上", Toast.LENGTH_SHORT).show();
                    }
                }, 4000);

            }
        });
        mSwipeRefreshLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        initData();
        return view;
    }

    private void initView() {
        tv_right_2.setText("开始拦截");
    }

    private void initData() {
        new HttpUtil.Builder("message/getMsgContentByMobile")
                .Params("mobile", "18584500435")
                .Success(new Success() {
                    @Override
                    public void Success(String model) {

                    }
                }).ErrorEventFunc(new ErrorFunListener() {

            @Override
            public void ErrorFun(Object... values) {

            }
        })
                .post();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    @OnClick({R.id.iv_smsSample, R.id.btn_sample, R.id.tv_right_2, R.id.iv_contast})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_smsSample:
                iv_smsSample.setVisibility(View.GONE);
                tv_noData.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_sample:
                iv_smsSample.setVisibility(View.VISIBLE);
                tv_noData.setVisibility(View.GONE);
                break;
            case R.id.tv_right_2:
                InputActivityUtil.gotoInputCodeActivity(getActivity());
                break;
            case R.id.iv_contast:
                startActivityForResult(new Intent(getActivity(), ContactsActivity.class), Config.REQUESTCODE_CONTASTS);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.REQUESTCODE_CONTASTS && resultCode == RESULT_OK) {

            et_inputNumber.setText(data.getStringExtra("phone"));
        }
    }
}